<?php
include_once "lib/config.php";
include_once "header.php";

if(isset($_SESSION['msg_success']) && $_SESSION['msg_success']!="")
{
	$msg_success=$_SESSION['msg_success'];
	unset($_SESSION['msg_success']);
}

if(isset($_SESSION['msg_error']) && $_SESSION['msg_error']!="")
{
	$msg_error=$_SESSION['msg_error'];
	unset($_SESSION['msg_error']);
}

if(isset($_POST['emailtemplate_id']) && $_POST['emailtemplate_id']>0 && isset($_POST['opt_status']) && $_POST['opt_status']!="")
{
	
	$arr_sanitize=array("status"=>$_POST['opt_status']);
	$pdo_obj->updatedata('emailtemplates',$arr_sanitize,array("id"=>$_POST['emailtemplate_id']));
	if($_POST['opt_status']=="Active")
	{
		$msg_success="Email Template activated successfully.";
	}
	if($_POST['opt_status']=="Inactive")
	{
		$msg_success="Email Template in-activated successfully.";
	}
	if($_POST['opt_status']=="Deleted")
	{
		$msg_success="Email Template Deleted successfully.";
	}
	
}

if(!isset($_POST['order_by']))
{
	$_POST['order_by']="asc";
}

if(!isset($_POST['type_title']))
{
	$_POST['type_title']="type";
}

if(!isset($_POST['pid']))	// code starts for implementing pagination 
{
	$_POST['pid']=1;
}

$condition=" emailtemplates.updated_by=users.id and temp_type='Email' ";

if($user_details[0]['user_type']!="Admin" && $user_details[0]['user_type']!="General Manager")
{
	$condition.=" and emailtemplates.status!='Deleted' ";
}

$conditionarr=array();

if(isset($_POST['searchText']) && $_POST['searchText']!="" && isset($_POST['filterby']) && $_POST['filterby']!="")
{
	$conditionarr[$_POST['filterby']]= $_POST['searchText'];
}

$totalrecords=DB::fetch_num_of_rows(DB::query("select emailtemplates.*,first_name,last_name from  emailtemplates,staffs where ".$condition,$conditionarr));

$emailtemplateinfo=DB::fetch_array(DB::query("select emailtemplates.*,first_name,last_name from  emailtemplates,staffs where ".$condition." order by ".$_POST['type_title']." ".$_POST['order_by']." limit ".(($_POST['pid']-1)*PAGELIMIT).", ".PAGELIMIT,$conditionarr));
//$emailtemplateinfo=$general_obj->search_recordbyparameter($pdo_obj," emailtemplates.*,first_name,last_name,ecode "," emailtemplates,users where ",$condition,$conditionarr,$_POST['type_title'],$_POST['order_by'],$_POST['pid']);


include_once "lib/paginate_class.php";


$paging_obj=new Paginate();

$pagingdiv=$paging_obj->getnewpager($_POST['pid'],$totalrecords);


?>
        
		<div class="container_12">
        
            <div class="bottom-spacing">
                	
                    <!-- Button 
                    <div >
                        <a href="addedit_emailtemplate.php" class="button" >
                        	<span>Add New Email Template <img src="images/plus-small.gif" width="12" height="9" alt="New Email Template" /></span>
                        </a>
                    </div>-->
                    <br>
                    <br>
                    <form name="mainform" method="post">
	                    <!-- Table records filtering -->
	                    Filter By: 
	                    <select class="input-short" style="width:200px;" name="filterby" id="filterby">
	                    	<option value="" selected="selected">Select filter</option>
	                        <option value="type"  <?php if(isset($_POST['filterby']) && $_POST['filterby']=="type"){ echo "selected"; } ?> >Email Template Type</option>
	                        
	                    </select>
	                    &nbsp;&nbsp;&nbsp;&nbsp;
	                    <input type="text" class="input-short" style="width:300px;" name="searchText" id="searchText" value="<?php if(isset($_POST['searchText'])){ echo $_POST['searchText']; } ?>">
	                    &nbsp;&nbsp;
	                    <input type="button" class="submit-green" value="Search" onclick="return getempfilterresults();"> &nbsp;&nbsp;<input type="button" class="submit-green" value="Show All" onclick="window.location.href='emailtemplates.php';">
	                    <input type="hidden" name="emailtemplate_id" id="emailtemplate_id">
	                    <input type="hidden" name="opt_status" id="opt_status">
						<input type="hidden" name="pid" id="pid" value="<?php if(isset($_POST['pid']))echo $_POST['pid']; ?>">
						<input type="hidden" name="type_title" id="type_title" value="<?php if(isset($_POST['type_title']))echo $_POST['type_title']; ?>">
						<input type="hidden" name="order_by" id="order_by" value="<?php if(isset($_POST['order_by']))echo $_POST['order_by']; ?>">
						
					</form>
                	<br>
            </div>
            
            
                <!-- Example table -->
                <div class="module" style="width:100% !important;">
                	<h2><span>Email Templates List</span></h2>
                    
                    <div class="module-table-body">
                    	
                        <table id="myTable" >
                        	<thead>
                                <tr>
                                    <!--<th style="width:3%;text-align:center !important;" ><input type="checkbox" onclick="return check_all(<?php //echo count($emailtemplateinfo); ?>);" id="checkAll"></th>-->
                                    <th style="width:10%">Sr.No.</th>
                                    <th style="width:15%"><a href="javascript:void(0)" onclick="return change_order('type','<?php echo $_POST['order_by'] ?>')" >Email Template Type</a></th>
                                    <th style="width:25%"><a href="javascript:void(0)" onclick="return change_order('subject','<?php echo $_POST['order_by'] ?>')" >Subject</a></th>
                                    <th style="width:10%"><a href="javascript:void(0)" onclick="return change_order('first_name','<?php echo $_POST['order_by'] ?>')" >Last Updated By</a></th>
                                    <th style="width:10%"><a href="javascript:void(0)" onclick="return change_order('update_date','<?php echo $_POST['order_by'] ?>')" >Last Updated Date</a></th>
                                    <th style="width:12%"><a href="javascript:void(0)" onclick="return change_order('status','<?php echo $_POST['order_by'] ?>')" >Status</a></th>
                                    <th style="width:18%">Actions</th>
                                </tr>
                            </thead>
                            <tbody>
                            <?php
                            if(count($emailtemplateinfo)>0)
                            {
                            	$count=0;
                            	for($i=0;$i<count($emailtemplateinfo);$i++)
                            	{
                            		$count++;
                            ?>
							    <tr <?php if($count%2==0)echo "class='odd'"; else echo "class='even'"; ?>>
                                    <td class="align-center"><?php echo ($i+1);  ?></td>
                                    <td><a href="javascript:void(0);" onclick="return editrecord(<?php echo $emailtemplateinfo[$i]['id']; ?>,'emailtemplate_id','addedit_emailtemplate.php');"><?php echo $emailtemplateinfo[$i]['type']; ?></a></td>
                                    <td><?php echo substr(trim($emailtemplateinfo[$i]['subject']),0,100); ?></td>
                                    <td><?php echo trim($emailtemplateinfo[$i]['first_name']." ".$emailtemplateinfo[$i]['last_name'])." ( ".$emailtemplateinfo[$i]['ecode']." ) "; ?></td>
                                    <td><?php echo date("d/m/Y",$emailtemplateinfo[$i]['update_date']); ?></td>
                                    <td><?php echo trim($emailtemplateinfo[$i]['status']); ?></td>
                                    <td>
                                    	<?php if($emailtemplateinfo[$i]['status']=="Inactive" || $emailtemplateinfo[$i]['status']=="Deleted"){  ?><a href="javascript:void(0);" onclick="return performoperation('active the emailtemplate','Active',<?php echo $emailtemplateinfo[$i]['id']; ?>,'emailtemplate_id');"><img src="images/tick-circle.gif" width="16" height="16" title="Activate" /></a><?php }else{ ?>
                                    	<a href="javascript:void(0);" onclick="return performoperation(' in-active the emailtemplate ','Inactive',<?php echo $emailtemplateinfo[$i]['id']; ?>,'emailtemplate_id');"><img src="images/minus-circle.gif" width="16" height="16" title="Inactivate" /></a><?php } ?>
                                        <a href="javascript:void(0);" onclick="return editrecord(<?php echo $emailtemplateinfo[$i]['id']; ?>,'emailtemplate_id','addedit_emailtemplate.php');"><img src="images/pencil.gif" width="16" height="16" title="Edit" /></a>
                                        <?php if($_SESSION['type_id']==2){  ?><a href="javascript:void(0);" onclick="return performoperation('delete the emailtemplate','Deleted',<?php echo $emailtemplateinfo[$i]['id']; ?>,'emailtemplate_id');"><img src="images/bin.gif" width="16" height="16" title="Delete" /></a><?php } ?>
                                    </td>
                                </tr>
                            <?php
                            	}
                            }
                            else
                            {
								?>
								<tr>
									<td colspan="7"><span class="redspan">There is no record found!!!</span></td>
								</tr>
								<?php	
							}
                            ?>
                            </tbody>
                        </table>
                        <br>
                        <?php echo $pagingdiv; ?>
                        
                        <div style="clear: both"></div>
                     </div> <!-- End .module-table-body -->
                </div> <!-- End .module -->
                
                
            
            <div style="clear:both;"></div>
            
            
        </div> <!-- End .container_12 -->
		
<?php include_once("footer.php"); ?>   
<?php
if(isset($msg_success) && $msg_success!="")
{
	$notymessage=$msg_success;
	$msgtype="success";
}
else if(isset($msg_error) && $msg_error!="")
{
	$msgtype="error";
	$notymessage=$msg_error;
}
else
{
	$msgtype="";
}
if($msgtype!="")
{
?>        
<script language="javascript">
	$(document).ready(function(){
		
		var options = $.parseJSON('{"text":"<?php echo $notymessage; ?>","layout":"top","type":"<?php echo $msgtype; ?>"}');
		noty(options);
		
	});
</script>  
<?php
}
?>     